import axios from './http'
import store from './store'
import * as types from './types'
import {Message} from 'element-ui'
import {ServerError} from './error'

export class Urls {
  // captcha的地址不能写死 http 或者 https
  static captcha = '//captcha.luosimao.com/static/dist/api.js'
  // todo 上线后修改地址为 http://cdn.nicesite.vip
  static img = 'http://p3z15mvur.bkt.clouddn.com/'
}

export class AccountService {
  static login (auth) {
    return axios.post('login', auth)
      .then(res => {
        if (res.data) {
          loginCommit(res.data)
        }
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static register (auth) {
    return axios.post('register', auth)
      .then(res => {
        if (res.data) {
          loginCommit(res.data)
          UserService.me()
        }
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static logout () {
    return axios.get('token/logout')
      .then(res => {
        store.commit(types.LOGOUT)
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static resetPass (auth) {
    return axios.post('reset/pass', auth)
      .then(res => {
        console.log(res)
        if (res.data === 'success') {
          store.commit(types.LOGOUT)
        }
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static refresh () {
    if (store.state.exp - new Date().getTime() > 3 * 3600 * 1000) {
      return
    }
    return axios.get('token/refresh')
      .then(res => {
        if (res.data) {
          loginCommit(res.data)
        }
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }
}

function loginCommit (auth) {
  store.commit(types.LOGIN, auth.jwt)
  store.commit(types.EXP, auth.exp)
}

export class UserService {
  static me () {
    return axios.get('user/me')
      .then(res => {
        if (res.data) {
          let user = res.data.user
          user.role = res.data.role
          user.username = user.user_name
          store.commit(types.USER, user)
        }
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static page (num, size) {
    return axios.post('user/page', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static online () {
    return !!store.state.jwt
  }
}

export class SecurityService {
  static qiniuToken () {
    return axios.get('token/qiniu')
      .then(res => {
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }
}

export class PaperService {
  static one (id) {
    return axios.post('one', {
      id: parseInt(id),
      item: 'paper'
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static page (num, size) {
    return axios.post('list', {
      item: 'paper',
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static create (name) {
    return axios.post('create', {
      item: 'paper',
      paper: {
        Name: name,
        MinLevel: 4
      }
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static addSid (sid) {

  }
}

export class SectionService {
  static one (id) {
    return axios.post('one', {
      id: parseInt(id),
      item: 'section'
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static page (num, size, pid) {
    return axios.post('list', {
      item: 'section',
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true,
      pid: parseInt(pid)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static create (name, pid) {
    return axios.post('create', {
      item: 'section',
      pid: parseInt(pid),
      section: {
        Name: name
      }
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }
}

export class QuestionService {
  static one (id) {
    return axios.post('one', {
      id: parseInt(id),
      item: 'question'
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static page (num, size, sid) {
    return axios.post('list', {
      item: 'question',
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true,
      sid: parseInt(sid)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static pic (num, size) {
    return axios.post('pic', {
      page_num: parseInt(num),
      page_size: parseInt(size)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static create (question, sid) {
    return axios.post('create', {
      item: 'question',
      sid: parseInt(sid),
      question: question
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static change (question) {
    return axios.post('change', {
      item: 'question',
      question: question
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static search (search) {
    return axios.post('search', {
      item: 'question',
      search: search
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }
}

export class LocalService {
  static SaveTmpMsg (title, content) {
    localStorage.msgTitle = title
    localStorage.msgContent = content
  }

  static GetTmpMsg () {
    return {
      title: localStorage.msgTitle,
      content: localStorage.msgContent
    }
  }

  static SaveTmpPost (title, content, module) {
    localStorage.postTitle = title
    localStorage.postContent = content
    localStorage.postModule = module
  }

  static GetTmpPost () {
    return {
      title: localStorage.postTitle,
      content: localStorage.postContent,
      module: localStorage.postModule
    }
  }

  static SaveIndexPath (path) {
    localStorage.indexPath = path
  }

  static GetIndexPath () {
    return localStorage.indexPath
  }

  static SaveUserPath (path) {
    localStorage.userPath = path
  }

  static GetUserPath () {
    return localStorage.userPath
  }
}
